<?php
/*
 * Created on Dec 2, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

include_once("CouponImpl.php");						//--> coupon impl
include_once("CouponCustomer.php");
include_once("CouponDomain.php");					//--> coupon domain

class Cart
{
	const STATUS_LOOKED 	= 1;
	const STATUS_CHANGED 	= 2;
	
	private $status 		= 0;
	private $productlist 	= array();
	
	private $coupon;
	
	public function Cart()
	{
		
	}
	
	public function setStatus($status)
	{
		$this->status = $status;
	}
	
	public function getStatus()
	{
		return $this->status;
	}
	
	public function setCoupon($coupon)
	{
		$this->coupon = $coupon;
	}
	
	public function getCoupon()
	{
		return $this->coupon;
	}
	
	public function setProduct($objProduct)
	{
		$this->productlist[] = $objProduct;
	}
	
	public function removeProduct($productID)
	{
		unset($this->productlist[$productID]);
	}
	
	public function getProductList()
	{
		return $this->productlist;
	}
	
	public function getProduct($key)
	{
		return $this->productlist[$key];
	}
	
	public function getProductCount()
	{
		return count($this->productlist);
	}
	
	public function isExisted($key)
	{
		if(array_key_exists($key, $this->productlist))
			return true;
		else
			return false;
	}
	
	public function getAmount()
	{
		$amount = $this->getTotal();
		
		$saveDollar = $this->getSaveOff();
		if($amount > $saveDollar)
			$amount -= $saveDollar;
		
		return $amount;
	}
	
	public function getTotal()
	{
		$amount = 0;
		
		foreach($this->productlist as $product){
			$amount	+= $product->getFinalPrice() * $product->getQuantity();
			$amount	+= $product->getAttributePrice();
		}
		
		return $amount;
	}
	
	public function getSaveOff()
	{
		$saveDollar = 0;
		
		if(is_object($this->coupon) && !$this->coupon->isExpire()){
			$total 	= $this->getTotal();
			
			if($this->coupon->isPercent()){
				$percentOff = $this->coupon->getOffValue();

				if($percentOff <= 100){
					$saveDollar = round($total * ($percentOff / 100), 2);
				}
			}else if($this->coupon->isSaveOff()){
				$saeDollar = $this->coupon->getOffValue();
			}
		}
		
		$saveDollar = round($saveDollar / 100, 2);
		
		return $saveDollar * 100;
	}
}
?>
